{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #N-Repeated Element in Size 2N Array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #array #hash-table"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数组 #哈希表"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: repeatedNTimes"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #在长度 2N 的数组中找出重复 N 次的元素"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数数组 <code>nums</code> ，该数组具有以下属性：</p>\n",
    "\n",
    "<div class=\"original__bRMd\">\n",
    "<div>\n",
    "<ul>\n",
    "\t<li><code>nums.length == 2 * n</code>.</li>\n",
    "\t<li><code>nums</code> 包含 <code>n + 1</code> 个 <strong>不同的</strong> 元素</li>\n",
    "\t<li><code>nums</code> 中恰有一个元素重复 <code>n</code> 次</li>\n",
    "</ul>\n",
    "\n",
    "<p>找出并返回重复了 <code>n</code><em> </em>次的那个元素。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [1,2,3,3]\n",
    "<strong>输出：</strong>3\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [2,1,2,5,3,2]\n",
    "<strong>输出：</strong>2\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>nums = [5,1,5,2,5,3,5,4]\n",
    "<strong>输出：</strong>5\n",
    "</pre>\n",
    "</div>\n",
    "</div>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= n &lt;= 5000</code></li>\n",
    "\t<li><code>nums.length == 2 * n</code></li>\n",
    "\t<li><code>0 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>\n",
    "\t<li><code>nums</code> 由 <code>n + 1</code> 个<strong> 不同的</strong> 元素组成，且其中一个元素恰好重复 <code>n</code> 次</li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [n-repeated-element-in-size-2n-array](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [n-repeated-element-in-size-2n-array](https://leetcode.cn/problems/n-repeated-element-in-size-2n-array/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['[1,2,3,3]', '[2,1,2,5,3,2]', '[5,1,5,2,5,3,5,4]']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        # A.sort()\n",
    "        # middle = A[len(A)//2]\n",
    "        # if A.count(middle) > 1:\n",
    "        #     return middle\n",
    "        # return A[len(A)//2-1]\n",
    "        \n",
    "        return int((sum(A) - sum(set(A))) / (len(A)/ 2 -1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 961. 重复 N 次的元素\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tempDict = {}\n",
    "        n = len(A)/2\n",
    "        while len(A):\n",
    "            tempC = A[0]\n",
    "            count = 1\n",
    "            A.remove(tempC)\n",
    "            tempDict[tempC] = count\n",
    "            while tempC in A:\n",
    "                count += 1\n",
    "                A.remove(tempC)\n",
    "                tempDict[tempC] = count\n",
    "            if count == n:\n",
    "                # print(count,n)\n",
    "                return tempC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        for i in range(len(A)):\n",
    "            ans=A.pop()\n",
    "            if ans in A:\n",
    "                return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        a, b = A[0], A[1]\n",
    "        if a == b:\n",
    "            return a\n",
    "        c = A[2]\n",
    "        i = 3\n",
    "        while i < len(A):\n",
    "            d = A[i]\n",
    "            if d == a or d == b or d == c:\n",
    "                return d\n",
    "            if c == a or c == b:\n",
    "                return c\n",
    "            c = A[i+1]\n",
    "            i+=2\n",
    "            \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: 'List[int]') -> 'int':\n",
    "        return (sum(A)-sum(set(A)))//(len(A)//2-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        for i in A:\n",
    "            if A.count(i)==len(A)*0.5:\n",
    "                break\n",
    "        return i\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: 'List[int]') -> 'int':\n",
    "        a=set()\n",
    "        for i in A:\n",
    "            if i in a:\n",
    "                return i\n",
    "            else:\n",
    "                a.add(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        result = dict()\n",
    "        t = len(A) / 2\n",
    "        for a in A:\n",
    "            n = result.get(a,0) + 1\n",
    "            if n == t:\n",
    "                return a\n",
    "            result[a] = n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        wc = collections.Counter(A)\n",
    "        N = len(A) // 2\n",
    "        for k, v in wc.items():\n",
    "            if v == N:\n",
    "                return k\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: 'List[int]') -> 'int':\n",
    "        map = {}\n",
    "        for i in A:\n",
    "            map[str(i)] = map.get(str(i), 0) + 1\n",
    "        for i in map.keys():\n",
    "            if map.get(i) == len(A)/2:\n",
    "                return int(i)\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        l=list((A))\n",
    "        d=list(set(A))\n",
    "        a=[0,0]\n",
    "        for i in d:\n",
    "            c=l.count(i)\n",
    "            if c>a[1]:\n",
    "                a[0]=i\n",
    "                a[1]=c\n",
    "        return (a[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        count = collections.Counter(A)\n",
    "        for k,v in count.items():\n",
    "            if v>1:\n",
    "                return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        import collections\n",
    "        count=collections.Counter(A)\n",
    "        print(count)\n",
    "        for k in count:\n",
    "            if count[k]>1:\n",
    "                return k\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "# 961. 重复 N 次的元素\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        tempDict = {}\n",
    "        n = len(A)/2\n",
    "        aSet = set(A)\n",
    "        print(aSet)\n",
    "        for tempC in aSet:\n",
    "            count = 1\n",
    "            A.remove(tempC)\n",
    "            tempDict[tempC] = count\n",
    "            while tempC in A:\n",
    "                count += 1\n",
    "                A.remove(tempC)\n",
    "                tempDict[tempC] = count\n",
    "            if count == n:\n",
    "                # print(count,n)\n",
    "                return tempC"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        N=len(A)//2\n",
    "        dict1=dict(Counter(A))\n",
    "        return [i for i in A if dict1[i]==N][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from collections import Counter\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        B = set(A)\n",
    "    \n",
    "        N = len(B)-1\n",
    "        \n",
    "        dict = {}\n",
    "        for key in A:\n",
    "            dict[key] = dict.get(key, 0) + 1\n",
    "        \n",
    "        for a in dict:\n",
    "            if dict[a] == N:\n",
    "                return a\n",
    "        \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        dic = dict(collections.Counter(A))\n",
    "        lst = list(dic.items())\n",
    "        lst.sort(key = lambda x:x[1],reverse = True)\n",
    "        return lst[0][0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        \n",
    "        from collections import Counter\n",
    "        dic = Counter(A)\n",
    "        for item in list(set(A)):\n",
    "            if dic[item] != 1:\n",
    "                return item"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A):\n",
    "        \"\"\"\n",
    "        :type A: List[int]\n",
    "        :rtype: int\n",
    "        \"\"\"\n",
    "        import re\n",
    "        alen = len(A)\n",
    "        if 4<=alen<=10000 and alen % 2 ==0:\n",
    "            s = ''.join(['-'+str(x)+'-' for x in A])\n",
    "            for i in range(alen):\n",
    "                n = re.findall('-'+str(A[i])+'-',s)\n",
    "                if len(n)>1:\n",
    "                    return A[i] "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: 'List[int]') -> 'int':\n",
    "        \n",
    "        a = set()\n",
    "        for i in range(len(A)):\n",
    "            if A[i] not in a:\n",
    "                a.add(A[i])\n",
    "            else:\n",
    "                return A[i]\n",
    "        \n",
    "        \n",
    "        # a = list(set(A))\n",
    "        # res = dict.fromkeys(a, 0)\n",
    "        # for i in A:\n",
    "        #     res[i] += 1 \n",
    "        # return max(res, key = res.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: 'List[int]') -> 'int':\n",
    "        a = list(set(A))\n",
    "        res = dict.fromkeys(a, 0)\n",
    "        for i in A:\n",
    "            res[i] += 1 \n",
    "        return max(res, key = res.get)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        max_num, count = None, 0\n",
    "        hashmap = {}\n",
    "        for num in A:\n",
    "            if num in hashmap:\n",
    "                hashmap[num] += 1\n",
    "            else:\n",
    "                hashmap[num] = 1\n",
    "            if hashmap[num] > count:\n",
    "                count = hashmap[num] \n",
    "                max_num = num\n",
    "        return max_num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        from collections import Counter\n",
    "        ac = Counter(A)\n",
    "        lenOfA = len(A)\n",
    "        for k,v in ac.items():\n",
    "            if v == lenOfA / 2:\n",
    "                return k"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        # 首先想到dict\n",
    "        # new_dict = {}\n",
    "        # for item in A:\n",
    "        #     if item in new_dict:\n",
    "        #         return item\n",
    "        #     else:\n",
    "        #         new_dict[item] = 1\n",
    "        # 官方说法\n",
    "        for k in range(1, 4):\n",
    "            for i in range(len(A) - k):\n",
    "                if A[i] == A[i+k]:\n",
    "                    return A[i]\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        A.sort()\n",
    "        N = len(A)//2\n",
    "        for i in range(len(A)-N+1):\n",
    "            if A[i] == A[i+N-1]:\n",
    "                return A[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        A.sort()\n",
    "        return A[len(A)//2-1] if A.count(A[len(A)//2-1]) >= A.count(A[len(A)//2]) else A[len(A)//2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        n = len(A)\n",
    "        for i in range(n-1):\n",
    "            for j in range(i+1, i+4): # O(3n), O(1)\n",
    "                if j < n and A[i] == A[j]:\n",
    "                    return A[i]\n",
    "        pass\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        a = sum(A)\n",
    "        seta = set(A)\n",
    "        for i in seta:\n",
    "            a -= i\n",
    "        \n",
    "        return a // (len(A) // 2 -1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        ans_hash=collections.defaultdict(int)\n",
    "        for i in A:\n",
    "            ans_hash[i]+=1\n",
    "        for ch in ans_hash.keys():\n",
    "            if ans_hash[ch] == len(set(A))-1:\n",
    "                return ch\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, A: List[int]) -> int:\n",
    "        length = len(A)//2\n",
    "        set_A = set(A)\n",
    "        res = 0\n",
    "        for i in set_A:\n",
    "            if A.count(i) == length:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        s = set()\n",
    "        for val in nums:\n",
    "            if val in s:\n",
    "                return (val)\n",
    "            s.add(val)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        for i in range(len(nums)):\n",
    "            for j in range(i + 1, len(nums)):\n",
    "                if nums[i] == nums[j]:\n",
    "                    return nums[i]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        n = len(nums)\n",
    "        for i in nums:\n",
    "            c = nums.count(i)\n",
    "            if c == n // 2:\n",
    "                return i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "        res = dict()\n",
    "        n = len(nums)//2\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in res.keys():\n",
    "                res.setdefault(nums[i],1)\n",
    "            else:\n",
    "               return nums[i]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        res = dict()\n",
    "        n = len(nums)//2\n",
    "        for i in range(len(nums)):\n",
    "            if nums[i] not in res.keys():\n",
    "                res.setdefault(nums[i],1)\n",
    "            else:\n",
    "                res[nums[i]]+=1\n",
    "            if res[nums[i]] == n:\n",
    "                return nums[i]\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        return sorted(Counter(nums).items(), key=lambda x: -x[1])[0][0]\n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        elements = set()\n",
    "        for num in nums:\n",
    "            if num in elements:\n",
    "                return num\n",
    "            elements.add(num)\n",
    "        return -1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        have=[]\n",
    "        for i in nums:\n",
    "            if i in have:\n",
    "                return i\n",
    "            have.append(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        n = len(nums) // 2\n",
    "        temp = {}\n",
    "        for i in nums:\n",
    "            if i in temp:\n",
    "                temp[i] += 1\n",
    "            else:\n",
    "                temp[i] = 1\n",
    "        for key, value in temp.items():\n",
    "            if value == n:\n",
    "                return key"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def repeatedNTimes(self, nums: List[int]) -> int:\n",
    "        n = len(nums) // 2\n",
    "        from collections import Counter\n",
    "        hm = Counter(nums)\n",
    "        for i, v in hm.items():\n",
    "            if v == n: return i"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
